#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int judge_char(char t)
{
    if ((t>='a'&&t<='z')||(t>='A'&&t<='Z')||(t>='0'&&t<='9')) return 1;
    else return 0;
}

char* str2num(char* a, int *num) //start point, return num;
{
	int t=0,tmp[10],p=1,if_minus=0;
	char *k=a;
	while (*k<'0'||*k>'9')
	{
		if(*k=='\0') return k;
		k++;
	}
	if(*(k-1)=='-') if_minus=1;
	while (*k>='0' && *k<='9') tmp[t++]=*(k++)-'0';
	t=t-1;
	*num=0;
	while(t>=0)
	{
		*num+=tmp[t]*p;
		t--;
		p*=10;
	}
	if(if_minus==1) *num=-*num;
	return k; //return final point
}

int int_set[999]={0}, index=0;
int if_int_word(char* str)
{
    int if_int_flag=1; 
    for (int i=0;i<strlen(str);i++) if(str[i]<'0'||str[i]>'9') if_int_flag=0; 
    if(if_int_flag==1) 
    {
        str2num(str,&int_set[index++]); //
        return 1;
    }
    else return 0;
}

int letters[60]={0};
int letter_stat(char c, int stat[])
{
    if (c>='a'&&c<='z') {stat[1+c-'a']++; return 1;}
    if (c>='A'&&c<='Z') {stat[27+c-'A']++; return 1;}
    return 0;
}

char longest_word[200][200]={0};
int max_word_length=0, m=0;

int read_text (const char* filePath = "passage.txt")
{
    FILE* fp = fopen(filePath,"r");
    if(fp==NULL) {printf("- Fail to open the document \"%s\". \n",filePath); return 0;}
    else printf("- Successfully opened the document \"%s\". \n",filePath);

    char ch;
    ch = fgetc(fp);
    char tmp_word[200], *p;
    int m = 0, tot_words = 0;
    printf("All possible words: \n");
    while (ch!=EOF) 
    {
        //1. read all possible 'word's
        //words, words with hyphen, words with numbers, numbers, words with ''' 
        
        while (ch != EOF && judge_char(ch) == 0) ch = fgetc(fp);
        if (ch == EOF) break;
        
        p = tmp_word;
        while (ch != EOF && (judge_char(ch) == 1 || ch == '-' || ch == '\'')) 
        {
            *p++ = ch;
            letter_stat(ch, letters);
            ch = fgetc(fp);
        }
        //final touch
        if (*(p-1)=='-') *(p-1) = '\0'; //delete wrond hyphen, if exist 
        else *p = '\0'; 

        //2. statics
        //strcpy(words[m++],tmp_word);
        tot_words++;
        if_int_word(tmp_word); //intger statics

        //show words;
        printf("%s ",tmp_word);

        //3. update longest words
        int t_length=strlen(tmp_word); 
        if (t_length>=max_word_length)
        {
            if (t_length==max_word_length) //if the word is of the same legth ... 
            {
                int if_exist = 0; 
                for (int i=0;i<m;i++)
                if(strcmp(longest_word[i],tmp_word)==0)
                {
                    if_exist = 1; // if the same word already appeared
                    break;
                }
                if (if_exist == 0) strcpy(longest_word[m++],tmp_word);
            }
            else //if the word is longer than the previous longest
            { 
                m=0;
                max_word_length=t_length;
                strcpy(longest_word[m++],tmp_word);
            }
        }
    }

    printf("\n");
    if (fclose(fp)!=0) printf("- Unable to close the file!\n\n");
    else printf("- Successfully finish reading the file! \n\n");

    printf("# %d words has been readed!\n\n",tot_words);

    printf("- Longest word(s): \n");
    for(int i=0;i<m;i++) printf("- %s\n",longest_word[i]);
    printf("# Longets word's length: %d\n",max_word_length);

    return tot_words;
}
int main()
{
    int tot_words = read_text("passage.txt");
    printf ("\n");

    printf("Letters statics: \n");
    for (int i=1;i<=26;i++)
    {
        printf("%c: %5d | %c: %5d\n",('a'+i-1),letters[i],('A'+i-1),letters[26+i]);
    }
    printf ("\n");

    printf("# Integers found: ");
    for (int i=0;i<index;i++) printf("%d ",int_set[i]);
    return 0;
}